package com.airticketbooking.controller.manager;

import com.airticketbooking.common.domain.PageDTO;
import com.airticketbooking.common.domain.PageQuery;
import com.airticketbooking.common.domain.R;
import com.airticketbooking.po.User;
import com.airticketbooking.vo.UserPageVO;
import com.airticketbooking.service.UserService;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

/**
 * @author Mr.J
 * @version 1.0.0
 * @date 2024/10/12
 * @desc
 */
@RequestMapping("/air/manager/user")
@RestController
@Api(tags = "管理端管理用户相关接口")
@RequiredArgsConstructor
@Slf4j
public class ManagerUserController {


	private final UserService userService;


	//	根据id获取用户信息接口
	@GetMapping("/getUserById/{userId}")
	@ApiOperation(value = "根据id获取用户信息接口")
	public R<User> getUserById(
			@NotBlank(message = "用户id不能为空")
			@PathVariable Integer userId) {

		User user = userService.getUserById(userId);

		if (user == null) {
			return R.error("用户不存在");
		}
		return R.ok(user);
	}

//	修改用户数据接口

	@PutMapping("/openOrBanUser/{userId}")
	@ApiOperation(value = "禁用/启用用户")
	public R<String> openOrBanUser(@PathVariable Integer userId) {


		try {
			userService.openOrBanUser(userId);
			return R.ok("用户禁用成功");
		} catch (Exception e) {
			log.error("禁用/启用用户失败", e);
			return R.error("禁用/启用用户失败");
		}


	}

	//	用户分页查询接口
	@GetMapping("/getUserByPage")
	@ApiOperation(value = "用户分页查询接口")
	public R<PageDTO<UserPageVO>> getUserByPage(PageQuery pageQuery) {

		Page<UserPageVO> page = userService.getUserByPage(pageQuery);

		PageDTO<UserPageVO> pageDTO = new PageDTO<>();
		pageDTO.setList(page.getResult());
		pageDTO.setTotal(page.getTotal());


		return R.ok(pageDTO);
	}


}
